
ARCH = arm-linux-

CC = $(ARCH)gcc
AR = $(ARCH)ar

CMAKE = @make -C

ECHO = @echo
CP = @cp -raf
REMOVE = @rm -rf
MKDIR = @mkdir -p

TARGET = $(PWD)/target

LIBZLIB = $(PWD)/zlib/lib/libz.a
INCZLIB = $(PWD)/zlib/include

LIBSPARSE = $(PWD)/platform/system/core/libsparse/libsparse.a
INCSPARSE = $(PWD)/platform/system/core/libsparse/include

MAKE_EXT4FS = $(PWD)/platform/system/extras/ext4_utils/make_ext4fs

MALL = $(TARGET) $(LIBZLIB) $(LIBSPARSE) $(MAKE_EXT4FS)

export CC
export AR

export TARGET

export LIBZLIB
export INCZLIB

export LIBSPARSE
export INCSPARSE

all: $(MALL)

$(TARGET):
	$(MKDIR) $(PWD)/target

$(LIBZLIB): $(PWD)/zlib-1.2.8.tar.xz
	$(ECHO) "##########################################################"
	$(ECHO) "Unpacking zlib-1.2.8..."
	$(REMOVE) $(PWD)/zlib/ $(PWD)/zlib-1.2.8/ $(PWD)/zlib.sh
	$(MKDIR) $(PWD)/zlib
	@tar xvf $(PWD)/zlib-1.2.8.tar.xz > /dev/null 2>&1
	$(ECHO) "Configureing zlib-1.2.8..."
	$(ECHO) "#/bin/sh" > $(PWD)/zlib.sh
	$(ECHO) "cd $(PWD)/zlib-1.2.8/" >> $(PWD)/zlib.sh
	$(ECHO) "$(PWD)/zlib-1.2.8/configure --prefix=$(PWD)/zlib --static" >> $(PWD)/zlib.sh
	$(ECHO) "make CC=$(CC)" >> $(PWD)/zlib.sh
	$(ECHO) "Building zlib-1.2.8..."
	@chmod 0755 $(PWD)/zlib.sh
	@$(PWD)/zlib.sh > /dev/null 2>&1
	$(ECHO) "Installing zlib-1.2.8..."
	@$(CMAKE) $(PWD)/zlib-1.2.8/ install > /dev/null 2>&1
	$(ECHO) "Cleaning cache..."
	$(REMOVE) $(PWD)/zlib-1.2.8/ $(PWD)/zlib.sh
	$(ECHO) "Done..."
	$(ECHO) "##########################################################"
	@sleep 1

$(LIBSPARSE): $(PWD)/platform/system/core/libsparse/Makefile
	$(ECHO) "##########################################################"
	$(ECHO) "Building libsparse..."
	$(CMAKE) $(PWD)/platform/system/core/libsparse/
	$(ECHO) "Done..."
	$(ECHO) "##########################################################"

$(MAKE_EXT4FS): $(PWD)/platform/system/extras/ext4_utils/Makefile
	$(ECHO) "##########################################################"
	$(ECHO) "Building make_ext4fs..."
	$(CMAKE) $(PWD)/platform/system/extras/ext4_utils/
	$(ECHO) "Done..."
	$(ECHO) "##########################################################"

clean:
	$(ECHO) "##########################################################"
	$(ECHO) "Clean All..."
	$(CMAKE) $(PWD)/platform/system/core/libsparse/ clean
	$(CMAKE) $(PWD)/platform/system/extras/ext4_utils/ clean
	$(REMOVE) $(MALL)
	$(REMOVE) $(PWD)/zlib/
	$(ECHO) "Done..."
	$(ECHO) "##########################################################"